﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;

namespace SinaBlogQiangShafa
{
   public class DataAccess
    {
       private string dbPath = null;
       public DataAccess()
       {
             
           System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
           string strBuildPath = a.Location;
           int iStrLength = strBuildPath.LastIndexOf("\\");
           dbPath = strBuildPath.Substring(0, iStrLength + 1);   //得到安装路径
        
       }
        public LinkedList<string> GetAll(string title)
        {
            string path = dbPath + title + ".xml";



            try
            {
                if (File.Exists(path))
                {
                    object obj = SerializationHelper.Deserialize(path);

                    return obj as LinkedList<string>;
                }
                else
                {
                    return null;
                }

            }
            catch (Exception err)
            {
                throw new Exception(err.ToString());
            }
          
           
           
            
        }
        private void Delete_DB(string title)
        {
            try
            {
                if (File.Exists(dbPath + title + ".xml"))
                {
                    System.IO.File.Delete(dbPath + title + ".xml");
                }
            }
            catch (Exception error)
            {
                Log.WriteError(string.Format("[{0}]:{1}", DateTime.Now.ToString(), error.ToString()));
            }

        }
        public void Update_DB(string title, LinkedList<string> stack)
        {
            string path = dbPath + title + ".xml";
           
            Delete_DB(path);
            try
            {
                SerializationHelper.Serialize(stack, path);
            }
            catch (Exception error)
            {
                Log.WriteError(string.Format("[{0}]:{1}", DateTime.Now.ToString(), error.ToString()));
            }
           
        }

       
    }
}
